package com.xgh.boss;

/**
 * @ClassName DeathLock
 * @Description
 * @Author xinggh
 * @Date 2020/8/13 23:03
 * @Version 1.0
 **/
public class DeathLock {

    public static void main(String[] args) {
        Object a = new Object();
        Object b = new Object();


        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {


                synchronized (a) {
                    System.out.println(Thread.currentThread().getName() + "尝试获取a 成功");
                    try {
                        Thread.sleep(2000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    System.out.println(Thread.currentThread().getName() + "尝试获取b ...");
                    synchronized (b) {
                        System.out.println(Thread.currentThread().getName() + "尝试获取b 成功");
                    }
                }
            }
        });

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {


                synchronized (b) {
                    System.out.println(Thread.currentThread().getName() + "尝试获取b 成功");
                    try {
                        Thread.sleep(2000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "尝试获取a ...");
                    synchronized (a) {
                        System.out.println(Thread.currentThread().getName() + "尝试获取a 成功");
                    }
                }
            }
        });

        t1.start();
        t2.start();

    }
}
